//
//  OldDetailViewController.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/9/4.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "OldDetailViewController.h"
#import "KJBannerHeader.h"

@interface OldDetailViewController () <UITableViewDelegate>

@property (nonatomic, strong) UILabel *lbTitle;
@property (nonatomic, strong) UILabel *lbDetail;
@property (nonatomic, strong) UILabel *lbThree;

@property (nonatomic, strong) UILabel *lbBottomTitle;
@property (nonatomic, strong) UILabel *lbBottomDetail;
@property (nonatomic, strong) UIImageView *backgroundImage;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *tabHeaderView;

@property (nonatomic, strong) NSMutableArray *arrData;

@property (nonatomic, strong) NSMutableArray *imageData;

@property (nonatomic, strong) NSArray *tagData;

@property (nonatomic, strong) KJBannerView *banner;
/**
 标签buttn
 */
@property (nonatomic, strong) UILabel *lbTag;
/**
 标签buttn
 */
@property (nonatomic, strong) DRNRealTimeBlurView *blurView;


@end

@implementation OldDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

/// 状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    [self.view addSubview:self.tableView];
    [self addNav];
   
    [self loadNetData];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)loadNetData {
    NSDictionary *par = @{@"comeAcrossId":self.comeAcrossId};
    
    [SXLoadingView showProgressHUD:@""];
    [PPNetworkHelper POST:URL_comeAcrossNext parameters:par success:^(id responseObject) {
        self.arrData = responseObject[@"retBody"][@"rows"];
        for (int i = 0; i < self.arrData.count; i++) {
            [self.imageData addObject:self.arrData[i][@"imgPath"]];
        }
        self.banner.imageDatas = self.imageData;
        self.tagData = self.arrData[0][@"tag"];
        
        [self updateUIWith:0];
        [SXLoadingView hideProgressHUD];
        
    } failure:^(NSError *error) {
        [SXLoadingView showAlertHUD:SXLoadingErrorStr duration:SXLoadingErrorTime];
    }];
}

- (void)updateUIWith:(NSInteger)index {
    [self.backgroundImage sd_setImageWithURL:[NSURL URLWithString:DSStringValue(self.imageData[index])] placeholderImage:LHYPlaceholderImage];
    
    self.lbTitle.text = DSStringValue(self.arrData[index][@"title"]);
    self.lbDetail.text = DSStringValue(self.arrData[index][@"referral"]);
    
    self.lbBottomTitle.text = DSStringValue(self.arrData[index][@"imgTitle"]);
    self.lbBottomDetail.text = DSStringValue(self.arrData[index][@"imgReferral"]);
    
    
    CGSize maximumLabelSize = CGSizeMake(SCREEN_WIDTH - 52, 9999);
    
    CGSize expectSize = [self.lbDetail sizeThatFits:maximumLabelSize];
    CGSize expectSizeBottom = [self.lbBottomDetail sizeThatFits:maximumLabelSize];
    
    self.lbDetail.frame = CGRectMake(26, GetViewYH(self.lbTitle) + 12, expectSize.width, expectSize.height);
    self.banner.frame = CGRectMake(0, GetViewYH(self.lbDetail) + 69, SCREEN_WIDTH, 342);
    self.lbBottomTitle.frame = CGRectMake(26, GetViewYH(self.banner) + 20, SCREEN_WIDTH - 52, 30);
    self.lbBottomDetail.frame = CGRectMake(26, GetViewYH(self.lbBottomTitle) + 9, SCREEN_WIDTH - 52, expectSizeBottom.height);
    
    CGFloat lastXW = 0;
    for (int i = 0; i < self.tagData.count; i++) {
        
        NSString *lbText = self.tagData[i];
        CGFloat weight = [self getWidthWithStr:lbText] + 14;
       
        self.lbTag = [self.view viewWithTag:i + 200];
        self.lbTag.tag = i + 200;
        self.lbTag.frame = CGRectMake(26 + lastXW, GetViewYH(self.lbDetail) + 19, weight, 27);
        lastXW = GetViewXW(self.lbTag);
        self.lbTag.text = lbText;
    }
    self.tabHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, GetViewYH(self.lbBottomDetail));
}

/**
 获取字符串长度

 @param text 字符串
 @return 字符串长度
 */
- (CGFloat)getWidthWithStr:(NSString *)text {
    CGFloat width = [text boundingRectWithSize:CGSizeMake(KScreenWidth, 27) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]} context:nil].size.width;
    return width;
}

- (DRNRealTimeBlurView *)blurView {
    if (!_blurView) {
        _blurView = [[DRNRealTimeBlurView alloc] initWithFrame:self.backgroundImage.bounds];
    }
    return _blurView;
}

- (void)initUI {
    self.tabHeaderView = [[UIView alloc] init];
    self.backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 455/375)];
    self.backgroundImage.userInteractionEnabled = YES;
    [self.view addSubview:self.backgroundImage];
    [self.backgroundImage addSubview:self.blurView];
//    渐变色
    UIView *gradientView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_WIDTH * 290/375, SCREEN_WIDTH, SCREEN_WIDTH * 165/375)];
    [gradientView gradientViewColorArray:@[(__bridge id)[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.0].CGColor,
                                           (__bridge id)[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor]
                                pointArr:@[@(0.5),
                                           @(0),
                                           @(0.5f),
                                           @(0.7)]];
    gradientView.layer.cornerRadius = 0;
    gradientView.layer.masksToBounds = NO;
    [self.blurView addSubview:gradientView];
    
    self.lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(26, 26, SCREEN_WIDTH - 52, 37)];
    self.lbDetail = [[UILabel alloc] init];
    self.lbDetail.numberOfLines = 0;
    CGSize maximumLabelSize = CGSizeMake(SCREEN_WIDTH - 52, SCREEN_HEIGHT);
    
    CGSize expectSize = [self.lbDetail sizeThatFits:maximumLabelSize];
    self.lbDetail.frame = CGRectMake(26, GetViewYH(self.lbTitle) + 12, expectSize.width, expectSize.height);
    
    KJBannerView *banner2 = [[KJBannerView alloc] initWithFrame:CGRectMake(0, GetViewYH(self.lbDetail) + 120, SCREEN_WIDTH, 342)];
    self.banner = banner2;
    banner2.imgCornerRadius = 5;
    banner2.isZoom = YES;
    banner2.itemSpace = -15;
    banner2.itemWidth = 282;
    banner2.infiniteLoop = NO;
    banner2.autoScroll = NO;
    banner2.imageType = KJBannerViewImageTypeNetIamge;//网络图片
    [self.view addSubview:banner2];
    
    WeakSelf;
    banner2.kSelectBlock = ^(KJBannerView * _Nonnull banner, NSInteger idx) {
        StrongSelf;
        NSString *str = self.arrData[idx][@"linkPath"];
        if (!IsNilString(str)) {
            WKWebViewController *vc = [[WKWebViewController alloc] initWithURLString:self.arrData[idx][@"linkPath"]];
            [strongSelf.navigationController pushViewController:vc animated:YES];
        }
    };
    banner2.kScrollEndBlock = ^(KJBannerView * _Nonnull banner, NSInteger index) {
        StrongSelf;
        [strongSelf updateUIWith:index];
    };
    
    self.lbBottomTitle = [[UILabel alloc] initWithFrame:CGRectMake(26, GetViewYH(banner2) + 20, SCREEN_WIDTH - 52, 30)];
    self.lbBottomDetail = [[UILabel alloc] initWithFrame:CGRectMake(26, GetViewYH(self.lbBottomTitle) + 9, SCREEN_WIDTH - 26 - 74, 80)];
    self.lbBottomDetail.numberOfLines = 0;
    
    [self.tabHeaderView addSubview:self.lbTitle];
    [self.tabHeaderView addSubview:self.lbDetail];
    [self.tabHeaderView addSubview:banner2];
    [self.tabHeaderView addSubview:self.lbBottomTitle];
    [self.tabHeaderView addSubview:self.lbBottomDetail];

    self.tabHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, GetViewYH(self.lbBottomDetail));
    
    self.lbTitle.backgroundColor = [UIColor clearColor];
    self.lbDetail.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor clearColor];
    
    for (int i = 0; i < 6; i++) {
        UILabel *lbTag = [[UILabel alloc] init];
        self.lbTag = lbTag;
        self.lbTag.tag = i + 200;
        lbTag.textAlignment = YES;
        lbTag.font = FONT(14);
        lbTag.textColor = [UIColor whiteColor];
        lbTag.backgroundColor = RGBACOLOR(255, 255, 255, 0.32f);
        lbTag.layer.cornerRadius = 13.5;
        lbTag.layer.masksToBounds = YES;
        [self.tabHeaderView addSubview:lbTag];
    }

    self.lbTitle.textColor = self.lbDetail.textColor = [UIColor whiteColor];
    self.lbTitle.font = FONT_BOLD(26);
    self.lbDetail.font = FONT(16);
    self.lbBottomTitle.font = FONT_BOLD(21);
    self.lbBottomDetail.font = FONT(13);
    self.lbBottomDetail.numberOfLines = 0;
    self.lbBottomDetail.textColor = UIColorFromRGB(0x333333);
}

- (void)addNav {
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(16, kNavBarAndStatusBarHeight - ADAPT_W(30), 34, 34)];
    [btn setImage:ImageNamed(@"navigationBar_popBack") forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)backAction {
    [super backAction];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.tableView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * 1.1);
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT - kNavBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.tabHeaderView;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}


- (NSMutableArray *)arrData {
    if (!_arrData) {
        _arrData = [[NSMutableArray alloc] init];
    }
    return _arrData;
}

- (NSMutableArray *)imageData {
    if (!_imageData) {
        _imageData = [[NSMutableArray alloc] init];
    }
    return _imageData;
}

@end



